package com.pocketsweet.ui;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.pocketsweet.R;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.ui.uilib.HeaderLayout.onRightContainerListener;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

/**
 * 
 * @author hantian 忘记密码-找回密码
 */
@ContentView(R.layout.activity_forgetpwd_find)
public class ForgetPassword extends Activity {

	@ViewInject(R.id.btn_sms_retry)
	private TextView btnSmsRetry;

	@ViewInject(R.id.etSmsCode)
	private EditText etSmsCode;

	@ViewInject(R.id.etMobile)
	private EditText etMobile;

	@ViewInject(R.id.mainHeader)
	private HeaderLayout header;
	private long mExitTime = 0;// 退出事件用到的事件

	private Timer timer;
	private final int TIMERMSG = 0x01;
	private int time = 60;
	private boolean getted = false;
	String smsCode;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case TIMERMSG:
				if (time != 0) {
					time--;
					btnSmsRetry.setText(time + "s后再次获取");
				} else if (time == 0) {
					timer.cancel();
					getted = false;
					btnSmsRetry.setText("获取验证码");
					time = 60;
				}
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		setHeaderFunction();
		Intent intent = getIntent();
		smsCode = intent.getStringExtra("smsCode");
		if (!StringUtils.isEmpty(smsCode)) {
			etSmsCode.setText(smsCode);
		}

	}

	public void setHeaderFunction() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setRightText("下一步");
		header.setLeftText("");
		header.setMiddleText("找回密码");
		header.setRighttContainerClickListener(new onRightContainerListener() {

			@Override
			public void onClick() {
				smsCode = etSmsCode.getText().toString();
				String moble = etMobile.getText().toString();
				if (TextUtils.isEmpty(moble)) {
					ToolKits.toast(ForgetPassword.this,
							R.string.moble_code_empty);
					return;
				}
				if (TextUtils.isEmpty(smsCode)) {
					ToolKits.toast(ForgetPassword.this, R.string.sms_code_empty);
					return;
				}
				Intent createInent = new Intent(ForgetPassword.this,
						CreateNewPassword.class);
				createInent.putExtra("smsCode", smsCode);
				ForgetPassword.this.startActivity(createInent);
			}
		});
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				Intent intent = new Intent(ForgetPassword.this, Login.class);
				ForgetPassword.this.startActivity(intent);
				ForgetPassword.this.finish();
			}
		});
	}

	@OnClick(R.id.btn_sms_retry)
	public void onSmsRetryClick(View view) {
		String phone = etMobile.getText().toString();
		if (TextUtils.isEmpty(phone)) {
			ToolKits.toast(this, "手机号码不能为空");
			return;
		}
		if (getted == true) {
			return;
		}
		getted = true;
		timer = new Timer();
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				Message message = new Message();
				message.what = TIMERMSG;
				handler.sendMessage(message);
			}
		}, 0, 1000);
		// 重置密码重新发送短信请参考下面的代码
		MLUser.requestPasswordResetBySmsCodeInBackground(etMobile.getText()
				.toString(), new RequestMobileCodeCallback() {
			@Override
			public void done(AVException e) {
				// 发送了验证码以后做点什么呢
				if (e != null) {
					ToolKits.toast(ForgetPassword.this, "" + e.getCode());
					ToolKits.toast(ForgetPassword.this, e.getMessage());
					return;
				}
				ToolKits.toast(ForgetPassword.this, R.string.sms_code_resend);
			}
		});
	}

	/**
	 * 重写onKeyDown方法
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			showExitAlet();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void showExitAlet() {
		if ((System.currentTimeMillis() - mExitTime) > 2000) {
			Toast toast = Toast.makeText(ForgetPassword.this, "再按一次退出程序",
					Toast.LENGTH_LONG);
			// 可以控制toast显示的位置
			toast.setGravity(Gravity.BOTTOM, 0, 150);
			toast.show();
			mExitTime = System.currentTimeMillis();

		} else {
			ForgetPassword.this.finish();
		}
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}

}
